<script setup>
import { ref } from 'vue'
import { getCodeAPI, userLoginAPI } from '@/api/'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter()
// 1. 登录
// 登录框
const onLoginDia = ref(true)
// 登录的参数
const formData = ref({
  username: '123456',
  password: '123456',
  code: '',
  codeid: ''
})

const onSubmit = async () => {
  const data = await userLoginAPI(formData.value)
  if (data) {
    ElMessage.success('登录成功')
    setTimeout(() => {
      router.push('/user')
    }, 800)
  }
}

// 1. 验证码处理
// 验证码图片
let src = ref('')
// 获取验证码
const getCode = async () => {
  const data = await getCodeAPI()
  src.value = data.captcha_img
  formData.value.codeid = data.captcha_id
}
getCode()
</script>

<template>
  <el-dialog
    v-model="onLoginDia"
    title="登录"
    :close-on-click-modal="false"
    width="700"
    class="login"
  >
    <el-form :model="formData" label-width="100px" style="max-width: 600px">
      <el-form-item label="账号">
        <el-input v-model="formData.username" placeholder="请输入账号" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="formData.password" placeholder="请输入密码" />
      </el-form-item>
      <el-form-item label="验证码">
        <el-col :span="11">
          <el-input v-model="formData.code" placeholder="请输入验证码" />
        </el-col>
        <el-col :span="11" class="code_img">
          <el-image :src="src">
            <template #placeholder>
              <div class="image-slot">Loading<span class="dot">...</span></div>
            </template>
          </el-image>
          <el-link type="info" @click="getCode">点击刷新</el-link>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<style lang="scss">
.login {
  .code_img {
    display: flex;
    margin-left: 20px;
  }
}
</style>
